import TTHotUpdate from "./TTHotUpdate"
const { ccclass, property } = cc._decorator;

@ccclass
class GameHotUpdateMgr extends cc.Component {
	static hotUpdateArr = {};//热更对象集合
	static updateQueue = [];//热更排队对列
	static addEvent = false;

	eventCustomALREADY_UP_TO_DATE = null;
	eventCustomUPDATE_FINISHED = null;
	eventCustomUPDATE_ERROR = null;
	eventCustomUPDATE_FAILED = null;
	start() {
		if (GameHotUpdateMgr.addEvent) return
		GameHotUpdateMgr.addEvent = true
		this.eventCustomALREADY_UP_TO_DATE = cc.vv.eventCustom.on("ALREADY_UP_TO_DATE", this.ALREADY_UP_TO_DATE.bind(this), this.node);
		this.eventCustomUPDATE_FINISHED = cc.vv.eventCustom.on("UPDATE_FINISHED", this.UPDATE_FINISHED.bind(this), this.node);
		this.eventCustomUPDATE_ERROR = cc.vv.eventCustom.on("UPDATE_ERROR", this.UPDATE_ERROR.bind(this), this.node);
		this.eventCustomUPDATE_FAILED = cc.vv.eventCustom.on("UPDATE_FAILED", this.UPDATE_FAILED.bind(this), this.node);
	}

	onDisable() {
		if (typeof this.eventCustomALREADY_UP_TO_DATE != "undefined") {
			cc.vv.eventCustom.off("ALREADY_UP_TO_DATE", this.eventCustomALREADY_UP_TO_DATE);
			this.eventCustomALREADY_UP_TO_DATE = undefined;
		}
		if (typeof this.eventCustomUPDATE_FINISHED != "undefined") {
			cc.vv.eventCustom.off("UPDATE_FINISHED", this.eventCustomUPDATE_FINISHED);
			this.eventCustomUPDATE_FINISHED = undefined;
		}
		if (typeof this.eventCustomUPDATE_ERROR != "undefined") {
			cc.vv.eventCustom.off("UPDATE_ERROR", this.eventCustomUPDATE_ERROR);
			this.eventCustomUPDATE_ERROR = undefined;
		}
		if (typeof this.eventCustomUPDATE_FAILED != "undefined") {
			cc.vv.eventCustom.off("UPDATE_FAILED", this.eventCustomUPDATE_FAILED);
			this.eventCustomUPDATE_FAILED = undefined;
		}
	}
	ALREADY_UP_TO_DATE(detail) {
		let mod = detail[0].moduleTmp;
		this.removeFromQueue(GameHotUpdateMgr.hotUpdateArr[mod]);
	}
	UPDATE_FINISHED(detail) {
		let mod = detail[0].moduleTmp;
		let chinesename = cc.vv.GLDefine.getChineseNameByEnglish(mod);
		cc.vv.GLUtils.showTips("游戏" + chinesename + "更新完毕!");
		this.removeFromQueue(GameHotUpdateMgr.hotUpdateArr[mod]);
	}
	UPDATE_FAILED(detail) {
		let mod = detail[0].moduleTmp;
		let chinesename = cc.vv.GLDefine.getChineseNameByEnglish(mod);
		cc.vv.GLUtils.showTips("游戏" + chinesename + "更新失败!,点击重新下载");
		this.removeFromQueue(GameHotUpdateMgr.hotUpdateArr[mod]);
	}
	UPDATE_ERROR(detail) {
		let mod = detail[0].moduleTmp;
		let chinesename = cc.vv.GLDefine.getChineseNameByEnglish(mod);
		cc.vv.GLUtils.showTips("游戏" + chinesename + "更新出错!,点击重新下载");
		this.removeFromQueue(GameHotUpdateMgr.hotUpdateArr[mod]);
	}
	UPDATE_PROGRESSION() {
	}
	UPDATE_VERSION() {
	}
	NEW_VERSION_FOUND() {
	}

	newUpdate(mod: string, d: {}) {
		if (!GameHotUpdateMgr.hotUpdateArr[mod]) {
			let update = new TTHotUpdate()
			update.addToQueue = () => {
				this.addToQueue(update)
			}
			update.changeQueue = () => {
				this.changeQueue(update)
			}
			update.init(d)
			GameHotUpdateMgr.hotUpdateArr[mod] = update
		}
		return GameHotUpdateMgr.hotUpdateArr[mod]
	}

	removeUpdate(mod: string) {
		GameHotUpdateMgr.hotUpdateArr[mod] = undefined
	}
	getUpdate(mod: string) {
		return GameHotUpdateMgr.hotUpdateArr[mod];
	}
	addToQueue(ttHotUpdateObj: TTHotUpdate) {
		for (let index = 0; index < GameHotUpdateMgr.updateQueue.length; index++) {
			if (ttHotUpdateObj == GameHotUpdateMgr.updateQueue[index]) {
				return
			}
		}
		GameHotUpdateMgr.updateQueue.push(ttHotUpdateObj)
		ttHotUpdateObj.setQueueStatus(true)
		if (GameHotUpdateMgr.updateQueue.length == 1) {
			this.startUpdateQueue()
		}
	}

	insertToQueue(ttHotUpdateObj: TTHotUpdate) {
		for (let index = GameHotUpdateMgr.updateQueue.length - 1; index >= 0; index--) {
			if (ttHotUpdateObj == GameHotUpdateMgr.updateQueue[index]) {
				cc.log("成功移除了一个");
				if (index == 0 || index == 1) {//正在下载或者排在第二个位置
					return;
				} else {
					GameHotUpdateMgr.updateQueue.splice(index, 1);
					break
				}
			}
		}

		if (GameHotUpdateMgr.updateQueue.length == 0) {
			GameHotUpdateMgr.updateQueue.push(ttHotUpdateObj);
		} else {
			GameHotUpdateMgr.updateQueue.splice(1, 0, ttHotUpdateObj);
		}
		ttHotUpdateObj.setQueueStatus(true);
		if (GameHotUpdateMgr.updateQueue.length == 1) {
			this.startUpdateQueue();
		}
	}

	changeQueue(ttHotUpdateObj: TTHotUpdate) {
		for (let index = 0; index < GameHotUpdateMgr.updateQueue.length; index++) {
			if (ttHotUpdateObj == GameHotUpdateMgr.updateQueue[index]) {
				//this.removeUpdate(mod);
				GameHotUpdateMgr.updateQueue.splice(index, 1)
				break
			}
		}
		GameHotUpdateMgr.updateQueue.splice(0, 0, ttHotUpdateObj)
	}

	removeFromQueue(ttHotUpdateObj: TTHotUpdate) {
		cc.log("更新结束了  从队列中移除");
		for (let index = GameHotUpdateMgr.updateQueue.length - 1; index >= 0; index--) {
			if (ttHotUpdateObj == GameHotUpdateMgr.updateQueue[index]) {
				cc.log("成功移除了一个");
				//this.removeUpdate(mod);
				GameHotUpdateMgr.updateQueue.splice(index, 1)
				break
			}
		}
		this.startUpdateQueue()
	}

	startUpdateQueue() {
		cc.log("开始新的下载 GameHotUpdateMgr.updateQueue.length is ", GameHotUpdateMgr.updateQueue.length)
		if (GameHotUpdateMgr.updateQueue.length > 0) {
			GameHotUpdateMgr.updateQueue[0].setQueueStatus(false)
			GameHotUpdateMgr.updateQueue[0].startUpdate()
		}
	}

}
export let gameHotUpdateMgr: GameHotUpdateMgr = new GameHotUpdateMgr();

